package test.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import test.bean.UserBean;
import test.filter.Const;
import test.service.LoginService;

public class SignInServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SignInServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	private UserBean ub=new UserBean();
	private LoginService ls=new LoginService();
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userName=request.getParameter("username");
		String passWord=request.getParameter("password");
		ub.setUserName(userName);
		ub.setPassWord(passWord);
		boolean result=ls.insertUser(ub);
		if(result){
			request.setAttribute("suc", "注册成功");
			//request.getSession().setAttribute(Const.USER_NAME,userName);
			request.getRequestDispatcher("/sign.jsp").forward(request, response);
		}else{
			request.setAttribute("suc", "注册失败");
			//request.getSession().setAttribute(Const.USER_NAME,userName);
			request.getRequestDispatcher("/sign.jsp").forward(request, response);
		}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
